using Microsoft.EntityFrameworkCore;
using MESData.ErrorCode;
using MESData.Infrastucture;
using BOMAPI.Services;
using Pomelo.EntityFrameworkCore.MySql;
using MESBaseis.Domain.BOM;

namespace BOMAPI
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.

            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();

            // 添加数据库上下文
            var conn = builder.Configuration.GetConnectionString("db");
            builder.Services.AddDbContext<MyDbcontext>(options =>
                options.UseMySql(conn, ServerVersion.AutoDetect(conn)));

            // CORS（如从浏览器跨域调用需要）
            builder.Services.AddCors(o =>
            {
                o.AddPolicy("AllowAll", p => p
                    .AllowAnyOrigin()
                    .AllowAnyHeader()
                    .AllowAnyMethod());
            });

       
            builder.Services.AddScoped<IManagementService, ManagementService>();

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            app.UseAuthorization();

            // 启用 CORS（如不需要可移除）
            app.UseCors("AllowAll");

            app.MapControllers();


            app.Run();
        }
    }
}
